<template>
  <div>
    <div class="top">
      <van-nav-bar title="当前城市-">
        <template #left>
          <van-icon name="cross" color="#ccc" size="20px" @click="cancle"/>
        </template>
      </van-nav-bar>
    </div>

    <div class="search">
      <form action="/">
        <van-search
          v-model="keyValue"
          placeholder="请输入城市名或拼音"
          background="#f4f4f4"
          @input="inputKey"
          show-action
        >
          <template #action>
            <div @click="onCancel" v-show="searchFlag">取消</div>
          </template>
        </van-search>
      </form>
    </div>
    <div class="inpSearch" v-show="searchFlag">
      <van-cell
        v-for="sea in search[0]"
        :key="sea.cityId"
        :title="sea.name"
        v-show="keyValue.length !== 0"
        @click='clickCity(sea.name)'
      />
      <img src="../../assets/nocity.png" v-show="imgLength" />
    </div>

    <div class="indexBar" v-show="!searchFlag">
      <div class="cityBtn">
        <p class="text">GPS定位你所在的城市</p>
        <button>定位失败</button>

        <div class="hotCity">
          <p class="text">热门城市</p>
          <button @click="clickCity(city.name)" v-for="city in hotCity" :key="city.cityId">
            {{ city.name }}
          </button>
        </div>
      </div>
      <van-index-bar>
        <div v-for="city in citys" :key="city.key">
          <van-index-anchor :index="city.key" />
          <van-cell v-for="f in city.find" :key="f.cityId" :title="f.name"  @click="clickCity(f)" />
        </div>
      </van-index-bar>
    </div>
  </div>
</template>

<script>
import Vue from "vue";
import { NavBar, Search, IndexBar, IndexAnchor, Cell, CellGroup } from "vant";
import { getsCity, getHotCity } from "@/request/request";

Vue.use(Cell);
Vue.use(CellGroup);
Vue.use(IndexBar);
Vue.use(IndexAnchor);
Vue.use(NavBar);
Vue.use(Search);

export default {
  created() {
    getsCity().then((citys) => {
      this.citys = citys;
    });

    getHotCity().then((res) => {
      this.hotCity = res;
    });
  },
  data() {
    return {
      keyValue: "",
      hotCity: [],
      citys: [],
      searchFlag: false,
      search: [],
      imgLength: false,
    };
  },
  methods: {
    inputKey() {
      this.search = [];
      this.searchFlag = true;
      this.citys.forEach((item) => {
        let res = item.find.filter((f) => {
          return f.name.includes(this.keyValue);
        });
        if (res.length !== 0) this.search.push(res);
      });

      if (this.search.length === 0) {
        this.imgLength = true;
      }
    },
    onCancel() {
      this.searchFlag = false;
      this.keyValue = "";
    },
    cancle(){
      this.$router.push('/movie')
    },
    clickCity(city){
      this.$store.commit('changeCity',city.name)
      this.$store.commit('changeCityId',city.cityId)

      this.$router.push('/movie')
    }
  },
};
</script>

<style lang="scss">
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}
.search {
  .van-search__content {
    background-color: #fff;
  }
}
.inpSearch img {
  position: relative;
  left: 50%;
  top: 50%;
  transform: translateX(-50%);
}
.cityBtn {
  padding: 10px 20px;
  .text {
    font-size: 13px;
    color: #797d82;
    margin: 0 0 10px;
  }
  button {
    height: 30px;
    line-height: 30px;
    text-align: center;
    width: 120px;
    border: none;
    margin: 5px 7.5px;
    color: #191a1b;
    font-size: 14px;
    background-color: #f4f4f4;
  }
}
</style>
